# 对 分布式进行权重配置，写入redis 后期从redis获取
import random

dicts = {
    "192.168.31.188": {
        "tags": {"main": 1, "para_url": 4}
    },
    "192.168.31.189": {
        "tags": {"main": 1, "para_url": 4}
    },
    "192.168.31.195": {
        "tags": {"main": 1, "para_url": 4}
    },
    "192.168.31.174": {
        "tags": {"main": 1, "para_url": 4}
    },
    "192.168.31.179": {
        "tags": {"main": 1, "para_url": 4}
    },
    "192.168.31.202": {
        "tags": {"main": 1, "para_url": 20}
    },
    "192.168.30.108": {
        "tags": {"main": 1, "para_url": 20}
    },
    "192.168.30.108:8001": {
        "tags": {"main": 1, "para_url": 20}
    },
    "192.168.31.21": {
        "tags": {"main": 1, "para_url": 20}
    },
}


def get_url_list(type_tag, active_url_list):
    lists = []
    for i in active_url_list:
        is_num = False
        ip = i.replace("http://", "").split(":")[0]
        if ip in dicts.keys():
            if type_tag in dicts[ip]["tags"].keys():
                num = dicts[ip]["tags"][type_tag]
                is_num = True
                lists.extend([i.replace("http://", "")] * num)
        # ip的优先级大于ip + 端口，同样的设置，ip优先生效
        if not is_num:
            ip_port = i.replace("http://", "")
            if ip_port in dicts.keys():
                if type_tag in dicts[ip_port]["tags"].keys():
                    num = dicts[ip_port]["tags"][type_tag]
                    is_num = True
                    lists.extend([i.replace("http://", "")] * num)
    random.shuffle(lists)
    return lists


# print(get_url_list("para_url", ["192.168.31.164:8001"]))
